//
//  ZLNewFeatureView.swift
//  ZLWeibo
//
//  Created by zhanglongtao on 16/12/26.
//  Copyright © 2016年 hanju001. All rights reserved.
//

import UIKit

class ZLNewFeatureView: UIView {

    @IBOutlet weak var scrollView: UIScrollView!
    
    @IBOutlet weak var enterButton: UIButton!
    
    @IBOutlet weak var pageControll: UIPageControl!
    
    
    @IBAction func enterStatus(_ sender: Any) {
        
        removeFromSuperview()
        
    }
    class func newFeatrue() -> ZLNewFeatureView{
        
        let nib = UINib(nibName: "ZLNewFeatureView", bundle: nil)
        
        let v: ZLNewFeatureView = nib.instantiate(withOwner: nil, options: nil)[0] as! ZLNewFeatureView
        
        v.frame = UIScreen.main.bounds
        
        return v
        
    }
    override func awakeFromNib() {
        
        
        //设置界面
        let count = 4
        let rect = UIScreen.main.bounds
        for i in 0..<count {
            
            let imageName = "new_feature_\(i + 1)"
            
            let iv = UIImageView(image: UIImage(named: imageName))
            //设置大小
            iv.frame = rect.offsetBy(dx: CGFloat(i) * rect.width, dy: 0)
            
            scrollView.addSubview(iv)
            
            
        }
        
        scrollView.bounces = false
        scrollView.isPagingEnabled = true
        scrollView.showsVerticalScrollIndicator = false
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.contentSize = CGSize(width: CGFloat(count + 1) * rect.width, height: rect.height)
        scrollView.delegate = self
        //隐藏button
        enterButton.isHidden = true
        
    }

}
extension ZLNewFeatureView: UIScrollViewDelegate{
    
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        //滚动到最后一屏视图删除
        let page = scrollView.contentOffset.x / scrollView.bounds.width
        
        //判断是否最后一页
        if Int(page) == scrollView.subviews.count {
            
            removeFromSuperview()
        }
        
        enterButton.isHidden = (Int(page) != scrollView.subviews.count - 1)
        
        
    }
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
        enterButton.isHidden = true
        
        let page = scrollView.contentOffset.x / scrollView.bounds.width + 0.5
        pageControll.currentPage = Int(page)
        
        pageControll.isHidden = (Int(page) == scrollView.subviews.count)
        
    }
    
}
